<?php

/**
 * This is the model class for table "word".
 *
 * The followings are the available columns in table 'word':
 * @property integer $id
 * @property string $word
 * @property string $symbol0
 * @property string $symbolA
 * @property string $symbolE
 * @property string $pronunciationA
 * @property string $pronunciationE
 */
class Word extends CActiveRecord
{
	private $pre;
	private $preInfo;
	private $sufInfo;
	private $sufClass;
	private $suf;
	public function getPre(){
		
	}
	public function getPreInfo(){
	
	}
	public function getSuf(){
	
	}
	public function getSufInfo(){
	
	}
	public function getSufClass(){
	
	}
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Word the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'word';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('word', 'required'),
			array('word', 'length', 'max'=>100),
		array('info', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, word', 'safe', 'on'=>'search'),
		);
	}
	
		public  function getRowFromDb($w){
			$row = new self();
			if($row = $row->find(
				"word = '".$w."'"
			)){
				
				return $row;
			}else{
				$this->word=$w;
				return $this;
			}
		
		
	}
	public function getDataFromInternet(){
		
		$zq = new Zq();
		if($row = $zq->youdao($this)){
			$zq->baidu($row);
		
			$zq->liju($row);
			return $row;
		}
		return null;
		
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			
			'yinghan'=>array(self::HAS_ONE, 'Dict', 'word_id','condition'=>'type=0'),
			'yy'=>array(self::HAS_ONE, 'Dict', 'word_id','condition'=>'type=1'),
			'd21'=>array(self::HAS_ONE, 'Dict', 'word_id','condition'=>'type=2'),
			'kl'=>array(self::HAS_ONE, 'Dict', 'word_id','condition'=>'type=3'),
			'youdaoP'=>array(self::HAS_MANY, 'Pronunciation', 'word_id','condition'=>'reference=0','order'=>'type ASC'),
			'baiduP'=>array(self::HAS_MANY, 'Pronunciation', 'word_id','condition'=>'reference=1'),
			
			'wPreInfo'=>array(self::HAS_MANY, 'WordPrefix', 'word_id'),
			'wSufInfo'=>array(self::HAS_MANY, 'WordSuffix', 'word_id'),
		'rootwords'=>array(
		self::HAS_MANY,
					 'WordRoot', 
					 'word_id','order'=>'rootwords.id ASC','limit'=>3),
	
			
		);
	}
	
	public function getExamples(){
		$criteria=new CDbCriteria;
		
		
		$criteria->compare('english'," ".$this->word." ",true);
		
		//$criteria->limit=10;
		$model= new Example();
		return new CActiveDataProvider($model, array(
					'criteria'=>$criteria,
		));
	}
	public function beforeSave(){
		$this->updated=time();
		return true;
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'word' => 'Word',
		'pre' => '前缀',
		'preInfo' => '前缀解释',
		'suf' => '后缀',
		'sufInfo' => '后缀解释',
		'sufClass' => '后缀词性',
			
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('word',$this->word,true);
		

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function searchWord()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
	
		
		$criteria->compare('word',$this->word);
	
	
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	public function getByRoot($root){
		$criteria=new CDbCriteria;
		
		$criteria->compare('id',$this->id);
		$criteria->compare('word',$this->word,true);
		
		
		return new CActiveDataProvider($this, array(
					'criteria'=>$criteria,
		));
	}
}
